//
//  LBRongYunKitLifeCycle.m
//  LBRongYunKit
//
//  Created by 刘文扬 on 2017/9/29.
//  Copyright © 2017年 lianj. All rights reserved.
//

#import "LBRongYunKitLifeCycle.h"
#import "LBRongYunKitHead.h"

@implementation LBRongYunKitLifeCycle

+ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    
    
    //融云IM
    [LBRCIMManager initializeRongYun];
    
    [LBRCIMManager configureRongYun];
    
    LBRCIMInfo *IMInfo = [LBRCIMManager shareInstance].rcIMInfo;
    
//    [LBRCIMManager connectRongYunWithToken:@"wXmalNC1GNY8JvLNzV2X7RBcd6JcQmKU6RYYgGnOAkPzBfkRxdyoPn/gJQAPbGH4kwXunOGIRq+gDdqlJA3r6nrk7VV8WNTfOuM/g6mPrDqlqV4c6GY5wDxJZsnPmJ9cOcQ67uGs4Xa0VBNku5MW3A=="];
    
    if (!IsStrEmpty(IMInfo.imToken)) {
        [LBRCIMManager connectRongYunWithToken:IMInfo.imToken];
    }else{
        [LBRCIMManager requestRongYunToken];
    }
    
    [LBRongYunKitLifeCycle registerUserNotificationSetting:application];
    
    return YES;
}

+(void)registerUserNotificationSetting:(UIApplication *)application
{
    if ([application
         respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        //注册推送, 用于iOS8以及iOS8之后的系统
        UIUserNotificationSettings *settings = [UIUserNotificationSettings
                                                settingsForTypes:(UIUserNotificationTypeBadge |
                                                                  UIUserNotificationTypeSound |
                                                                  UIUserNotificationTypeAlert)
                                                categories:nil];
        [application registerUserNotificationSettings:settings];
    }
}


/**
 *  APP在后台或者离线时收到的推送内容
 *
 *  @param application application
 *  @param userInfo    推送内容
 */
+ (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //程序在前台自动被调用,
    //在后台的时候点击状态栏调用
    
//    LBPushMessageModel *pushMessage = [LBPushMessageModel mj_objectWithKeyValues:userInfo];
//    [[LBPushManager sharedManager] handleRemoteNotificationWithInfo:pushMessage isFromMessageCenter:NO];
}

+ (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
    
    //程序在前台或者在后台未被杀死的情况调用,我已经在IM层过滤了前台的通知,所以这里只有后台点击的时候调用
    
    //程序在前台自动被调用,
    //在后台的时候点击状态栏调用
    
//    LBPushMessageModel *pushMessageModel = [LBPushMessageModel mj_objectWithKeyValues:notification.userInfo];
//
//    [[LBPushManager sharedManager] handleRemoteNotificationWithInfo:pushMessageModel isFromMessageCenter:NO];
    
    
    //    RCMessage *message = [LBRCIMManager getMessageByMId:pushMessageModel.rc.mId];
    //    NSLog(@"%@",message);
    //
    //    if (message != nil) {
    //        id extra = [message.content valueForKey:@"extra"];
    //        LBMessageExtraModel *appData = [LBMessageExtraModel mj_objectWithKeyValues:extra];
    //        pushMessageModel.appData = appData;
    //        [[LBPushManager sharedManager] handleRemoteNotificationWithInfo:pushMessageModel isFromMessageCenter:NO];
    //    }
    
    
    
}

/**
 * 推送处理2
 */
//注册用户通知设置
+ (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    // register to receive notifications
    [application registerForRemoteNotifications];
}
/**
 * 推送处理3
 */
+ (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [LBRCIMManager setDeviceToken:deviceToken];

}

@end
